package com.xxx.shetuan3.controller;

import com.github.pagehelper.PageInfo;
import com.xxx.shetuan3.common.CommonResult;
import com.xxx.shetuan3.dto.OrganizationDTO;
import com.xxx.shetuan3.exception.ApiException;
import com.xxx.shetuan3.pojo.Organization;
import com.xxx.shetuan3.service.OrganizationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/organization")
public class OrganizationController {
    @Autowired
    private OrganizationService organizationService;
    @ApiOperation(value = "添加一个社团")
    @PostMapping("")
    public CommonResult saveOrganization(@Valid OrganizationDTO organizationDTO){
        Organization organization = new Organization(organizationDTO);
        organization.setStart(new Date(System.currentTimeMillis()));
        boolean res = organizationService.saveOrganization(organization);
        if(res){
            return CommonResult.success("创建成功");
        }else{
            throw new ApiException("创建失败");
        }

    }

    @ApiOperation("重启社团")
    @PostMapping("/restart/{organizationId}")
    public CommonResult restartOrganizationById(@PathVariable("organizationId") Integer organizationId){
        Organization organization = organizationService.findOrganizationById(organizationId);
        if(organization==null){
            throw new ApiException("社团不存在");
        }else if(organization.getEnd()==null){
            throw new ApiException("该社团未被停止");
        }else{
            boolean res = organizationService.restartOrganization(organizationId);
            if(res){
                return CommonResult.success("重启成功");
            }else {
                throw new ApiException("重启失败");
            }
        }

    }

    @ApiOperation(value = "停止社团")
    @PostMapping("/stop/{organizationId}")
    public CommonResult stopOrganizationById(@PathVariable("organizationId") Integer organizationId){
        Organization organization = organizationService.findOrganizationById(organizationId);
        if(organization==null) {
            throw new ApiException("该社团不存在");
        }else if(organization.getEnd()!=null){
            throw new ApiException("该社团已经停止");
        }else{
            boolean res = organizationService.stopOrganization(organizationId);
            if(res){
                return CommonResult.success("停止成功");
            }else{
                throw new ApiException("停止失败");
            }
        }
    }

    @ApiOperation(value = "删除社团")
    @DeleteMapping("")
    public CommonResult deleteOrganization(Integer organizationId){
        Organization organization = organizationService.findOrganizationById(organizationId);
        if(organization==null){
            throw new ApiException("该社团不存在");
        }else{

            boolean res = organizationService.deleteOrganizationById(organizationId);
            if(res){
                return CommonResult.success("删除成功");
            }else{
                throw new ApiException("删除失败");
            }
        }
    }

    @ApiOperation(value = "更新社团")
    @PostMapping("/{organizationId}")
    public CommonResult updateOrganization(@PathVariable("organizationId") Integer organizationId,@Valid OrganizationDTO organizationDTO){
        Organization organization1 = organizationService.findOrganizationById(organizationId);
        if(organization1==null){
            throw new ApiException("社团不存在");
        }else{
            Organization organization2 = new Organization(organizationDTO);
            organization2.setOrganizationId(organizationId);
            boolean res = organizationService.updateOrganization(organization2);
            if(res){
                return CommonResult.success("更新成功");
            }else{
                throw new ApiException("更新失败");
            }
        }

    }

    @ApiOperation(value = "查找社团")
    @GetMapping("/{organizationId}")
    public CommonResult<Organization> findOrganizationById(@PathVariable("organizationId") Integer organizationId){
        Organization organization = organizationService.findOrganizationById(organizationId);
        if(organization!=null){
            return CommonResult.success(organization);
        }else{
            throw new ApiException("社团未找到");
        }

    }
    @ApiOperation(value = "分页测试")
    @GetMapping("/testpage")
    public CommonResult<PageInfo> pageTest(@RequestParam(value = "start",defaultValue = "1") Integer start,
                                           @RequestParam(value = "size",defaultValue = "10") Integer size){
        return CommonResult.success(organizationService.queryPage1(start,size));
    }

    @ApiOperation(value = "查询全部社团，分页功能稍等一会")
    @GetMapping("/list")
    public CommonResult<List<Organization>> findAll(){
        return CommonResult.success(organizationService.findAll());
    }

    @ApiOperation(value = "分页查询")
    @PostMapping("/page")
    public CommonResult<List<Organization>> queryPage(@RequestParam(value = "start",defaultValue = "1") Integer start,
                                                      @RequestParam(value = "size",defaultValue = "10") Integer size){
        return CommonResult.success(organizationService.queryPage(start,size));
    }
}
